package com.zy.flow.compment;

/**
 * @author hanhuafeng
 * @version V1.0
 * @description
 * @date 2023/5/31
 */

import com.yomahub.liteflow.annotation.LiteflowCmpDefine;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.annotation.LiteflowMethod;
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.enums.LiteFlowMethodEnum;
import com.zy.annotation.RuleCmp;
import com.zy.enums.ElNameEnum;
import com.zy.flow.bo.GetMethodBo;

import java.util.Map;

@LiteflowComponent
@LiteflowCmpDefine
@RuleCmp(params = GetMethodBo.class, flowCmpId = "getComponent", cmpType = ElNameEnum.COMMON, defaultText = "GET")
public class GetComponent {
    @LiteflowMethod(LiteFlowMethodEnum.BEFORE_PROCESS)
    public void beforeAcmp(NodeComponent bindCmp) {
        System.out.println("before A");
    }

    @LiteflowMethod(LiteFlowMethodEnum.PROCESS)
    public void processAcmp(NodeComponent bindCmp) {
        GetMethodBo getMethod = bindCmp.getCmpData(GetMethodBo.class);
        if (getMethod != null) {
            Map<String, Object> params = bindCmp.getRequestData();
            String url = getMethod.getUrl();
            for (Map.Entry<String, Object> entry : params.entrySet()) {
                System.out.println("key is " + entry.getKey() + " value is " + entry.getValue());
                if (entry.getValue() instanceof String param) {
                    url = url.replace("{" + entry.getKey() + "}", param);
                }
            }
            System.out.println("getMethod url is " + url);
        }
        System.out.println(getMethod);
        System.out.println("GetMethod executed!");
    }

    @LiteflowMethod(LiteFlowMethodEnum.IS_ACCESS)
    public boolean isAcmpAccess(NodeComponent bindCmp) {
        return true;
    }

    @LiteflowMethod(LiteFlowMethodEnum.ON_SUCCESS)
    public void onAcmpSuccess(NodeComponent bindCmp) {
        System.out.println("Acmp success");
    }

    @LiteflowMethod(LiteFlowMethodEnum.ON_ERROR)
    public void onAcmpError(NodeComponent bindCmp) {
        System.out.println("Acmp error");
    }

    @LiteflowMethod(LiteFlowMethodEnum.IS_END)
    public boolean isAcmpEnd(NodeComponent bindCmp) {
        return false;
    }
}
